package synchronizeddemo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Window extends Thread{
	static int ticket = 1;
	static Lock lock = new ReentrantLock();
	
	public Window(String name) {
		super(name);
	}
	@Override
	public void run() {
		while(ticket <= 20){
			lock.lock();
			// 加锁
			if (ticket <= 20) {
				System.out.println(this.getName() + ticket);
				ticket++;
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			// 解锁
			lock.unlock();
		}
	}
}
public class SaleTicketDemo {
	public static void main(String[] args) {
		Window w1 = new Window("A1:");
		Window w2 = new Window("A2:");
		Window w3 = new Window("A3:");
		Window w4 = new Window("A4:");
		
		w1.start();
		w2.start();
		w3.start();
		w4.start();
	}
}
